#ifndef TABLE_H
#define TABLE_H
#include "minidbms.h"
#include "parse.h"
#include <vector>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#define IBYTES 4
#define MAX 6
#include<sys/time.h>


class Table
{
public:
    Table();
    Table(Create_Stmt CreateData);
    //Intial the Table, establish its struction.
    void Insert(Insert_Stmt InsertData);
    //Insert tuple to table that corresponding its name or in default order.
    void Print(ofstream &Output);
    //Print the table's attribute names and all tuples.
    void Write(ofstream &Output);
    //void Write(FILE* fp);
    void Read(ifstream &fin);
    //void Read(FILE* fp);
    virtual ~Table();
    void DeleteTable();
    string GetName()
    {
        return TableName;
    };
    vector<void*>* GetTableData()
      {
        return TableData;
      }
    string* GetAttributeName()
    {
      return AttributeName;
    }
    int GetSize()
    {

      return TableSize;
    }
    int* GetType()
    {
        return  AttributeType;
    }
    int GetMax(int n)
    {
        return maxb[n];
    }
    int GetMin(int n)
    {
        return minb[n];
    }
private:
    bool CherkInsert(Insert_Stmt InsertData,int NameOrder[]);
    /*
      Check the insert tuple :
      1.Primary key(cannot be null or repeated).
      2.Data Type vaildation( e.g. cannot insert string into int).
      3.Column size vaildation(e.g. do not exceed the max size of varchar attribute).
    */
    string TableName;
    int TableSize;
    //vector<Sub_Stmt*>* TableData;
    vector<void*>* TableData;
    string* AttributeName;
    int* AttributeType;
    int* AttributeSize;
    bool* IsPrimary;
    vector<int> maxb;
    vector<int> minb;
};
void writestring(string &str, ofstream &out);
//void writestring(string &str, FILE* fp);
string* readstring(ifstream &in);
//string* readstring(FILE* fp);

using namespace std;
struct timer{
    public:
    struct timeval begin, end;
    timer(){ gettimeofday(&begin, NULL); }
    void start(){ gettimeofday(&begin, NULL); }
    void finish(){ gettimeofday(&end, NULL); }
    friend inline ostream & operator<<(ostream &os, timer &a){
        double use = 1000000 * (a.end.tv_sec - a.begin.tv_sec) + a.end.tv_usec - a.begin.tv_usec;
        use /= 1000000.0;
        os << use;
        return os;
    }
};

#endif // TABLE_H


